package com.changyi.device.printer;

import android.content.Context;
import android.os.RemoteException;
import android.util.Log;

import com.landicorp.android.eptapi.utils.QrCode;
import com.sunmi.peripheral.printer.InnerPrinterCallback;
import com.sunmi.peripheral.printer.InnerPrinterException;
import com.sunmi.peripheral.printer.InnerPrinterManager;
import com.sunmi.peripheral.printer.InnerResultCallbcak;
import com.sunmi.peripheral.printer.SunmiPrinterService;

import java.util.ArrayList;
import java.util.List;

public class SunmiP2Printer implements PrinterInterface{
    private Context mContext;
    private static SunmiP2Printer sunmiP2Printer;
    private SunmiPrinterService sunmiPrinterService;
    private List<String> mList;
    private String title;
    private String content;
    private String code;
    private String qRCode;
    private int feelLine;
    private List<Integer> typeList;
    private SunmiP2Printer(Context context) {
        mContext = context;
    }

    public static SunmiP2Printer getInstance(Context context) {
        if (sunmiP2Printer == null) {
            synchronized (SunmiP2Printer.class){
                if (sunmiP2Printer == null) {
                    sunmiP2Printer = new SunmiP2Printer(context);
                }
            }
        }
        return sunmiP2Printer;
    }


    @Override
    public PrinterInterface initDevice() {
        typeList=new ArrayList<>();
        bindPrintService();
        return sunmiP2Printer;
    }

    @Override
    public PrinterInterface printTitle(String title) {
        typeList.add(1);
           this.title=title;
        return sunmiP2Printer;
    }

    @Override
    public PrinterInterface printText(String text) {
        typeList.add(2);
        this.content=text;
        return sunmiP2Printer;
    }

    @Override
    public PrinterInterface printList(List<String> mList) {
        typeList.add(0);
        this.mList=mList;

        return sunmiP2Printer;
    }

    @Override
    public PrinterInterface printCode(String code) {
        typeList.add(3);
        this.code=code;
        return sunmiP2Printer;
    }

    @Override
    public PrinterInterface printQrCode(String qRCode) {
        typeList.add(4);
        this.qRCode=qRCode;
        return sunmiP2Printer;
    }

    @Override
    public PrinterInterface feedLine(int feelLine) {
        typeList.add(5);
        this.feelLine=feelLine;
        return sunmiP2Printer;
    }

    private void bindPrintService() {
        try {
            InnerPrinterManager.getInstance().bindService(mContext, new InnerPrinterCallback() {
                @Override
                protected void onConnected(SunmiPrinterService service) {
                    sunmiPrinterService = service;
                    for(int type=0;type<typeList.size();type++) {
                        switch (typeList.get(type)) {
                            case 0:
                                try {

                                    sunmiPrinterService.enterPrinterBuffer(true);
                                    for (String content : mList) {
                                        if (content.contains("|")) {
                                            sunmiPrinterService.lineWrap(1, innerResultCallbcak);
                                            String[] aa = content.split("\\|");
                                            for (int i = 0; i < aa.length; i++) {
                                                sunmiPrinterService.printTextWithFont(aa[i] + "\n", "", 25, innerResultCallbcak);
                                            }
                                        } else if (content.contains("@@@")) {
                                            String temp = content.substring(3, content.length());
                                            sunmiPrinterService.lineWrap(1, innerResultCallbcak);
                                            sunmiPrinterService.setAlignment(1, innerResultCallbcak);
                                            sunmiPrinterService.printQRCode(temp, 6, 2, innerResultCallbcak);
                                            sunmiPrinterService.setAlignment(0, innerResultCallbcak);
                                        } else if (content.contains("&&&")) {
                                            String temp = content.substring(3, content.length());
                                            sunmiPrinterService.lineWrap(1, innerResultCallbcak);
                                            //sunmiPrinterService.printBarCode(temp,50,60,300,50,null);
                                            sunmiPrinterService.printBitmap(ZXingUtils.creatBarcode(mContext, temp, 300, 60, false), innerResultCallbcak);
                                        } else {
                                            sunmiPrinterService.printTextWithFont(content.trim() + "\n", "", 25, innerResultCallbcak);
                                        }
                                    }
                                    sunmiPrinterService.lineWrap(3, innerResultCallbcak);
                                    sunmiPrinterService.exitPrinterBufferWithCallback(true, innerResultCallbcak);
                                } catch (RemoteException e) {
                                    e.printStackTrace();
                                }
                                break;
                            case 1:
                                try {
                                    sunmiPrinterService.printTextWithFont(title, "", 40, innerResultCallbcak);
                                    sunmiPrinterService.lineWrap(1, innerResultCallbcak);
                                } catch (RemoteException e) {
                                    e.printStackTrace();
                                }
                                break;
                            case 2:
                                try {
                                    sunmiPrinterService.printTextWithFont(content, "", 25, innerResultCallbcak);
                                } catch (RemoteException e) {
                                    e.printStackTrace();
                                }
                                break;
                            case 3:
                                try {
                                    sunmiPrinterService.printBitmap(ZXingUtils.creatBarcode(mContext, code, 300, 60, false), innerResultCallbcak);
                                    sunmiPrinterService.lineWrap(1, innerResultCallbcak);
                                } catch (RemoteException e) {
                                    e.printStackTrace();
                                }
                                break;
                            case 4:
                                try {
                                    sunmiPrinterService.printQRCode(qRCode, 6, 2, innerResultCallbcak);
                                    sunmiPrinterService.lineWrap(1, innerResultCallbcak);
                                } catch (RemoteException e) {
                                    e.printStackTrace();
                                }
                                break;
                            case 5:
                                try {
                                    sunmiPrinterService.lineWrap(feelLine, innerResultCallbcak);
                                } catch (RemoteException e) {
                                    e.printStackTrace();
                                }
                                break;
                        }
                    }
                }

                @Override
                protected void onDisconnected() {
                    sunmiPrinterService = null;
                }
            });
        } catch (InnerPrinterException e) {
            e.printStackTrace();
        }
    }

    private InnerResultCallbcak innerResultCallbcak = new InnerResultCallbcak() {
        @Override
        public void onRunResult(boolean isSuccess) {
            Log.e("lxy", "isSuccess:" + isSuccess);
        }

        @Override
        public void onReturnString(String result) {
            Log.e("lxy", "result:" + result);
        }

        @Override
        public void onRaiseException(int code, String msg) {
            Log.e("lxy", "code:" + code + ",msg:" + msg);
        }

        @Override
        public void onPrintResult(int code, String msg) {
            Log.e("lxy", "code:" + code + ",msg:" + msg);
        }
    };
}
